﻿using HandyControl.Controls;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows.Input;
using WWLauncher.Constant;
using WWLauncher.Libs.WindowsInput.Native;
using WWLauncher.Util;
using WWLauncher.View;
using WWLauncher.View.UserControls.Config;
using static WWLauncher.Util.GlobalHotKey;

namespace WWLauncher
{
    public partial class MainWindow
    {
        public static int colorPickerHotKeyId = -1;
        public static int hotKeyId = -1;
        public static int toDoHotKeyId = -1;

        #region 注册热键
        #region 注册当前窗口的热键
        /// <summary>
        /// 注册当前窗口的热键
        /// </summary>
        public void RegisterHotKey(bool first)
        {
            try
            {
                if (Config.HotkeyModifiers != GlobalHotKey.HotkeyModifiers.None)
                {
                    hotKeyId = GlobalHotKey.RegisterHotKey(Config.HotkeyModifiers, Config.Hotkey, () =>
                    {
                        if (RunTimeStatus.MainHotKeyDown) return;
                        RunTimeStatus.MainHotKeyDown = true;
                        new Thread(() =>
                        {
                            Thread.Sleep(RunTimeStatus.MainHotKeyTime);
                            RunTimeStatus.MainHotKeyDown = false;
                        }).Start();

                        if (MotionControl.HotkeyFinished)
                        {
                            if (CheckShouldShowApp())
                            {
                                DisplayApp();
                            }
                            else
                            {
                                HideApp();
                            }
                        }
                    });
                    if (!first)
                    {
                        Growl.Success("WWLauncher快捷键注册成功(" + Config.HotkeyStr + ")!", "HotKeyGrowl");
                    }
                }
                else
                {
                }
            }
            catch (Exception)
            {
                if (first)
                {
                    Growl.Error("WWLauncher启动快捷键已被其它程序占用(" + Config.HotkeyStr + ")!", "HotKeyGrowl");
                }
                else
                {
                    Growl.Error("WWLauncher启动快捷键已被其它程序占用(" + Config.HotkeyStr + ")!", "HotKeyGrowl");

                }
            }
        }
        #endregion


        /// <summary>
        /// 注册新建待办的热键
        /// </summary>
        public static void RegisterCreateToDoHotKey(bool first)
        {
            try
            {

                if (Config.HotkeyModifiers != GlobalHotKey.HotkeyModifiers.None)
                {
                    //加载完毕注册热键
                    toDoHotKeyId = GlobalHotKey.RegisterHotKey(Config.ToDoHotkeyModifiers, Config.ToDoHotkey, () =>
                    {
                        if (MotionControl.HotkeyFinished)
                        {
                            ToDoWindow.ShowOrHide();
                        }
                    });
                    if (!first)
                    {
                        Growl.Success("新建待办任务快捷键注册成功(" + Config.ToDoHotkeyStr + ")!", "HotKeyGrowl");
                    }
                }

            }
            catch (Exception)
            {
                if (first)
                {
                    Growl.Error("新建待办任务快捷键已被其它程序占用(" + Config.ToDoHotkeyStr + ")!", "HotKeyGrowl");
                }
                else
                {
                    Growl.Warning("新建待办任务快捷键已被其它程序占用(" + Config.ToDoHotkeyStr + ")!", "HotKeyGrowl");
                }
            }
        }

        /// <summary>
        /// 注册新建待办的热键
        /// </summary>
        public static void RegisterColorPickerHotKey(bool first)
        {
            try
            {
                if (Config.HotkeyModifiers != GlobalHotKey.HotkeyModifiers.None)
                {
                    //加载完毕注册热键
                    colorPickerHotKeyId = GlobalHotKey.RegisterHotKey(Config.ColorPickerHotkeyModifiers, Config.ColorPickerHotkey, () =>
                    {
                        if (MotionControl.HotkeyFinished)
                        {
                            GlobalColorPickerWindow.CreateNoShow();
                        }
                    });
                    if (!first)
                    {
                        Growl.Success("拾色器快捷键注册成功(" + Config.ColorPickerHotkeyStr + ")!", "HotKeyGrowl");
                    }
                }
            }
            catch (Exception)
            {
                if (first)
                {
                    Growl.Warning("拾色器快捷键已被其它程序占用(" + Config.ColorPickerHotkeyStr + ")!", "HotKeyGrowl");
                }
                else
                {
                    Growl.Warning("拾色器快捷键已被其它程序占用(" + Config.ColorPickerHotkeyStr + ")!", "HotKeyGrowl");
                }
            }
        }

        #region 注册关闭显示器热键
        /// <summary>
        /// 注册关闭显示器热键
        /// </summary>
        public void RegisteCloseMonitorHotKey()
        {
            try
            {

                hotKeyId = GlobalHotKey.RegisterHotKey(HotkeyModifiers.ModWin, Key.F3, () =>
                {
                    if (RunTimeStatus.MainHotKeyDown) return;
                    RunTimeStatus.MainHotKeyDown = true;
                    new Thread(() =>
                    {
                        Thread.Sleep(RunTimeStatus.MainHotKeyTime);
                        RunTimeStatus.MainHotKeyDown = false;
                    }).Start();

                    if (MotionControl.HotkeyFinished)
                    {
                        WinApi.PostMessage(WinApi.HWND_BROADCAST, WinApi.WM_SYSCOMMAND, (Int32)WinApi.SC_MONITORPOWER, WinApi.INT_MONITOROFF);


                    }
                });

            }
            catch (Exception)
            {
                {
                    Growl.Error("WWLauncher启动快捷键已被其它程序占用( Alt+F4 )!", "HotKeyGrowl");

                }
            }
        }
        #endregion

        #region 注册关闭RelicCoH2的热键
        /// <summary>
        /// 注册关闭RelicCoH2热键
        /// </summary>
        public void RegisteCloseRelicCoH2HotKey()
        {
            try
            {

                hotKeyId = GlobalHotKey.RegisterHotKey(HotkeyModifiers.ModWin, Key.F4, () =>
                {
                    if (RunTimeStatus.MainHotKeyDown) return;
                    RunTimeStatus.MainHotKeyDown = true;
                    new Thread(() =>
                    {
                        Thread.Sleep(RunTimeStatus.MainHotKeyTime);
                        RunTimeStatus.MainHotKeyDown = false;
                    }).Start();

                    if (MotionControl.HotkeyFinished)
                    {
                        var process = Process.GetProcessesByName("RelicCoH2").FirstOrDefault();
                        if (process != null)
                        {
                            process.Kill();
                        }

                    }
                });

            }
            catch (Exception)
            {
                {
                    Growl.Error("WWLauncher启动快捷键已被其它程序占用( Alt+F4 )!", "HotKeyGrowl");

                }
            }
        }
        #endregion

        #endregion

        private void MainWindow_OnMouseWheelDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (MotionControl.HotkeyFinished)
            {
                if (!this.IsVisible || this.Opacity == 0)
                {
                    this.DisplayApp();
                }
                else
                {
                    this.HideApp();
                }

            }
        }
    }
}
